
package org.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
/*As a new user when he or she wishes to register into the application, this class is called.
 * All the field that accepts information from the user such as his/her name, email, phone etc
 * are taken by this activity and then it saves all the relevant information to the remote database
 * */
public class NewUserSignInActivity extends Activity {
	private EditText E_Name, E_Password, E_Email, E_ID, E_Phone;
	private String Name, Password, ID;
	String Title, Author, Publisher, isbn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.newusersigninlayout);
		
		//Storing user information into variables to further pass it to the remote database
		E_Name = (EditText) findViewById(R.id.name);
		E_Password = (EditText) findViewById(R.id.password);
		E_Email = (EditText) findViewById(R.id.email);
		E_Phone = (EditText) findViewById(R.id.phone);
		E_ID = (EditText) findViewById(R.id.id);

		Name = E_Name.getText().toString();
		Password = E_Password.getText().toString();
		E_Email.getText().toString();
		E_Phone.getText().toString();
		ID = E_ID.getText().toString();

	}
	
	//The onClick method, called when the user clicks "Register me" on the UI
	/*Takes all the information feeded by the user and then saves it into the remote database
	 * */
	public void onClick(View view) {

		if (Name.equals("") || Password.equals("") || ID.equals(""))
			System.out.println("NULL entered");

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("Name", E_Name.getText()
				.toString()));
		postParameters.add(new BasicNameValuePair("Password", E_Password
				.getText().toString()));
		postParameters.add(new BasicNameValuePair("Email", E_Email.getText()
				.toString()));
		postParameters.add(new BasicNameValuePair("Phone", E_Phone.getText()
				.toString()));
		postParameters.add(new BasicNameValuePair("ID", E_ID.getText()
				.toString()));

		//Interaction with the remote database
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://hci.cs.sfsu.edu/~fall2010.03/Includes/AddNewStudentDetailsToDB.php");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(postParameters));

			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();

		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		Toast.makeText(this, "Details Added!!!! ", Toast.LENGTH_LONG).show();

		Intent i = new Intent(this, UserLogedInPageActivity.class);
		i.putExtra("ID", E_ID.getText().toString());

		startActivity(i);
	}

}
